<template>

	<view class="main-box">
		<view class="" @click="changes" v-if="adst==1">
			<image src="https://h5.sx2b2c.com/images/common/float-ad-icon.gif" mode="" class="adbox"></image>
		</view>
		<image src="../../static/sonx.png" mode="widthFix" class="images"></image>
		<view class="numbers">

			<image class="buttons" mode="widthFix" src="/static/btns.png" @tap="clcpic"></image>
			<view class="bottoms">
				放弃领取停车劵
			</view>


		</view>
		<view class="botm">

		</view>

	</view>
</template>

<script>
	import FingerprintJS from '@fingerprintjs/fingerprintjs' // 引入
	import {
		getip,
		getUrl,
		subOrder,
		getPhonetype,
		getlog,
		logs
	} from "../../api/index.js"

	import {
		debounce
	} from '../../utils/commo.js'
	export default {
		data() {
			return {
				deviceid:'',
				flag: false,
				mobile: "",
				show: true,
				a: '',
				param: "",
				orderNo: "", //订单号
				adst: 0, //信封显示与否
				url: ""
			};
		},
		onShow() {
		this.getvisitorId()
		},
		onLoad(option) {
			console.log(typeof(decodeURIComponent(window.location.href)))
			// alert(JSON.stringify(option))

			if (option.a) {
				let reg = RegExp(/extendParam/)
				console.log(reg.test(option.a)) //true
				var result = reg.test(option.a)
				if (result == true) {
					let a = this.$route.query.a.split("&extendParam=")
					console.log(a)
					this.a = a[0]
					console.log(this.a)
					console.log(JSON.parse(a[1]).adId)
					if (a.length > 1) {
						this.param = JSON.parse(a[1]).adId
					}
					const formDatax = new FormData();

					formDatax.append('a', this.a);
					fetch('https://www.sx2b2c.com/api/dhk3000/getAdUrl', {
							method: 'post',
							body: formDatax
						})
						.then(response => response.json())
						.then(data => {
							console.log(data)
							this.adst = data.data.adst
							this.url = data.data.url1 //信封跳转地址


						})
						.catch(error => console.error(error));
				} else {
					uni.setStorageSync('a', option.a)
					this.a = option.a
					const formDatax = new FormData();

					formDatax.append('a', uni.getStorageSync('a'));
					fetch('https://www.sx2b2c.com/api/dhk3000/getAdUrl', {
							method: 'post',
							body: formDatax
						})
						.then(response => response.json())
						.then(data => {
							console.log(data)
							this.adst = data.data.adst
							this.url = data.data.url1 //信封跳转地址


						})
						.catch(error => console.error(error));
				}
			}

if(option.requestId){
	
	let a={
		'requestId':option.requestId,
		'adId':option.adId,
		'pn':option.pn
	}
	if(a.requestId==null||a.requestId==undefined||a.requestId=='null'){
		a.requestId=''
	}
	if(a.adId==null||a.adId==undefined||a.adId=='null'){
		a.adId=''
	}
	if(a.pn==null||a.pn==undefined||a.pn=='null'){
		a.pn=''
	}
	console.log(JSON.stringify(a))
	
	uni.setStorageSync('param', JSON.stringify(a))
	this.param=uni.getStorageSync('param')
}else{
	if (option.urlCode) {
		this.param = option.urlCode
		uni.setStorageSync('param', option.urlCode)
	}
	if (option.adId) {
		this.param = option.adId
		uni.setStorageSync('param', option.adId)
	}
	
	
	if (option.tokenId) {
		this.param = option.tokenId
		uni.setStorageSync('param', option.tokenId)
	}
	if (option.clickid) {
		uni.setStorageSync('param', option.clickid)
		this.param = option.clickid
	}
	if (option.a_oId) {
		uni.setStorageSync('param', option.a_oId)
		this.param = option.a_oId
	}
	if (option.a_oid) {
		uni.setStorageSync('param', option.a_oid)
		this.param = option.a_oid
	}
	if (option.param) {
		uni.setStorageSync('param', option.param)
		this.param = option.param
	}
}
			
			console.log(this.param)

		},

		mounted() {
			//第一种方法
			// if(window.name == ""){
			// console.log("首次被加载");
			// window.name = "isReload"; // 在首次进入页面时我们可以给window.name设置一个固定值 
			// }else if(window.name == "isReload"){
			// console.log("页面被刷新");
			// }
			//第二种方法
			if (window.performance.navigation.type == 1) {
				console.log("页面被刷新")
			} else {
				console.log("首次被加载")
			}
		},

		methods: {
			async getvisitorId() {
				const fp = await FingerprintJS.load()
				const result = await fp.get()
				this.deviceid = result.visitorId
				logs({
					a: this.a,
					eventType: 1,
					xcx: 1,
					deviceid: this.deviceid
				}).then((res) => {
					console.log(res)
			
				})
				console.log(this.deviceid)
			},
			changes() {
				// 在这里编写处理点击事件的逻辑
				window.location.href = this.url
			},
			//监听手机号变化
			inputs() {
				console.log(this.mobile.length)
				if (this.mobile.length == 11) {
					this.clcpic()
				}

			},
			//获取订单号
			getRandomNum() {
				var a = new Date().getFullYear() + (new Date().getMonth() + 1).toString().padStart(2, "0") +
					new Date().getDate().toString().padStart(2, "0")
				//  "2020-02-12"   获取的是当前日期
				var b = new Date().getHours().toString().padStart(2, "0") + new Date().getMinutes().toString()
					.padStart(2, "0") + new Date().getSeconds().toString().padStart(2, "0")
				//11:13:02当前时间
				var randomNum = "" + Math.round(Math.random() * 900000000);
				while (randomNum.length < 8) {
					randomNum = "0" + randomNum;
				}
				const number = 'AD' + a + b + randomNum
				this.orderNo = number


				return randomNum;
			},


			//点击图片
			clcpic() {

				debounce(() => {
					console.log(`https://ulink.alipay.com/?scheme=` +
						encodeURIComponent(
							`alipays://platformapi/startapp?appId=2021004115620105&page=pages/unit/unit&query=${encodeURIComponent('mobile='+this.mobile+'&a='+this.a +'&param='+this.param+'&orderNo='+this.orderNo   )}`
						))

					logs({
						a: this.a,
						eventType: 2,
						xcx: 1,
						deviceid: this.deviceid
					}).then((res) => {
						console.log(res)
					})
					this.getRandomNum() //调用生成订单号
					getip().then((res) => {
						console.log(res.data.city)
						if (res.data.city == "深圳") {
							getUrl({
								a: this.a,
								param: this.param,
								mobile: this.mobile
							}).then((result) => {
								console.log(result.data.ad_h5_to_url)
								if (result.code == 1 && result.data.ad_h5_to_url != '') {
									window.location.href = result.data.ad_h5_to_url
								} else {
									uni.showToast({
										icon: 'none',
										title: '抱歉，当前暂无可领取福利',
										duration: 2000
									});
								}
							})

						} else {
							// #ifdef H5
							uni.setStorageSync('phones', this.mobile)
							if (/MicroMessenger/.test(window.navigator.userAgent)) {
								let system = navigator.userAgent.toLowerCase();
								//微信环境==苹果跳转支付宝
								if (/(iPhone|iPad|iPod|iOS)/i.test(system)) {
									subOrder({
										a: this.a,
										param: this.param,
										mobile: this.mobile,
										nomobile: '1',
										from_t: '1',
										from_env: '2',
										orderNo: this.orderNo
									}).then((res) => {
										window.location.href =
											`https://ulink.alipay.com/?scheme=` +
											encodeURIComponent(
												`alipays://platformapi/startapp?appId=2021004115620105&page=pages/banner/banner&query=${encodeURIComponent('mobile='+this.mobile+'&a='+this.a +'&param='+this.param+'&orderNo='+this.orderNo+'&deviceid='+this.deviceid   )}`
											)
									})

									// wx.navigateToMiniProgram({ appId: '2021004115620105'})
									// extraData: { // 可选，传递给支付宝小程序的额外数据 }, success(res) { // 跳转成功的回调函数 }, fail(res) { // 跳转失败的回调函数 } })
								} else {
									var ua = navigator.userAgent;
									console.log(ua);
									getPhonetype({
										ua: ua
									}).then((res) => {

								//微信环境==安卓跳转支付宝
								subOrder({
									a: this.a,
									param: this.param,
									mobile: this.mobile,
									nomobile: '1',
									from_t: '2',
									from_env: '2',
									orderNo: this.orderNo
								}).then((res) => {
									uni.navigateTo({
										url: "/pages/lead/lead?mobile=" +
											this.mobile +
											'&a=' + this
											.a +
											'&param=' +
											this
											.param +
											'&orderNo=' +
											this
											.orderNo+'&deviceid='+this.deviceid
									})
								})
									})





								}

							} else if (/AlipayClient/.test(window.navigator.userAgent)) {

								subOrder({
									a: this.a,
									param: this.param,
									mobile: this.mobile,
									nomobile: '1',
									from_t: '0',
									from_env: '1',
									orderNo: this.orderNo
								}).then((res) => {
									//支付宝环境
									window.location.href = `https://ulink.alipay.com/?scheme=` +
										encodeURIComponent(
											`alipays://platformapi/startapp?appId=2021004115620105&page=pages/banner/banner&query=${encodeURIComponent('mobile='+this.mobile+'&a='+this.a +'&param='+this.param+'&orderNo='+this.orderNo+'&deviceid='+this.deviceid   )}`
										)
								})


							} else {
								console.log(this.param)
								// 其他环境
								subOrder({
									a: this.a,
									param: this.param,
									mobile: this.mobile,
									nomobile: '2',
									from_t: '0',
									from_env: '0',
									orderNo: this.orderNo
								}).then((res) => {

									console.log(res)
									window.location.href = `https://ulink.alipay.com/?scheme=` +
										encodeURIComponent(
											`alipays://platformapi/startapp?appId=2021004115620105&page=pages/banner/banner&query=${encodeURIComponent('mobile='+this.mobile+'&a='+this.a +'&param='+this.param+'&orderNo='+this.orderNo+'&deviceid='+this.deviceid  )}`
										)
								})



							}
							// #endif
							// #ifdef MP-ALIPAY
							uni.navigateTo({
								url: "/pages/banner/banner"
							})
							my.setStorageSync({
								key: 'phone',
								data: this.mobile
							})
							// #endif

						}
					})
				}, 800)





			}

		}
	}
</script>

<style lang="scss">
	@keyframes butChange {
		0% {
			transform: scale(1);
		}

		50% {
			transform: scale(1.1);
		}

		100% {
			transform: scale(1);
		}
	}

	.adbox {
		position: fixed;
		bottom: 10%;
		right: 0;
		width: 100rpx;
		height: 100rpx;
		z-index: 9999;
	}

	.main-box {
		min-height: 100%;
		position: relative;
		background: linear-gradient(#8BD2FC, #8BD2FC);

	}

	.images {
		height: 750rpx;
		width: 100%;
	}


	.box {
		height: 20rpx;
	}

	.buttons {

		animation: butChange 1s infinite;
		margin: 0 auto;
		width: 100%;
		margin-top: 30rpx;
		z-index: 999999999;


	}

	.numbers {
		background-color: 8bd2fc;
		width: 100%;
		height: 35%;
		padding: 56rpx 70rpx 0 70rpx;
		margin: 0 auto;
	}

	.number {
		border-radius: 60rpx;
		border: 1px solid white;
		width: 100%;
		margin: 0 auto;
		font-size: 40rpx;
		text-indent: 40rpx
	}

	.bottoms {

		color: #EBF8FF;
		width: 100%;
		text-align: center;
		font-size: 36rpx;
	}

	.botm {
		height: 100rpx;
	}
</style>